﻿using System;
using System.Linq;
using System.Web.Mvc;
using WxxMail.BLL;
using WxxMail.Common;
using WxxMail.Domain;
using WxxMail.IBLL;
using WxxMail.MVC.Models;

namespace WxxMail.MVC.Factories
{
    public partial class AnswerFactory:IAnswerFactory
    {
        IAnswerService _answerService=new AnswerService();
        /// <summary>
        /// 转换成列表
        /// </summary>
        /// <param name="Request"></param>
        /// <returns></returns>
        public AnswerListModel PrepareAnswerListModel(AnswerRequest Request)
        {

            var model = new AnswerListModel();
            var anserlist = _answerService.GetList(Request);
            model.answerlist = anserlist
                .Select(x =>
                {
                    var answerModel = new AnswerModel();
                    PrepareAnswerModel(answerModel, x);
                    return answerModel;
                })
                .ToList();

            return model;
        }
        /// <summary>
        /// 将Model层中的Answer赋值给MVC层的Answer
        /// </summary>
        /// <param name="answerModel"></param>
        /// <param name="answer"></param>
        /// <returns></returns>
        public AnswerModel PrepareAnswerModel(AnswerModel answerModel, Answer answer)
        {
            if(answer==null)
                throw new ArgumentNullException("model");
            if (answerModel==null)
                throw new ArgumentNullException("model");
            answerModel.AnswerId = answer.AnswerId;
            answerModel.AnswerContent = answer.AnswerContent;
            answerModel.BestNum = answer.BestNum;
            return answerModel;
        }
        /// <summary>
        /// 将mvc层的AnswerModel转化成Model层的Answer
        /// </summary>
        /// <returns></returns>
        public Answer ToAnswer(AnswerModel answerModel, Answer answer)
        {
            answer.AnswerId = answerModel.AnswerId;
            answer.AnswerContent = answerModel.AnswerContent;
            answer.BestNum = answerModel.BestNum;
            return answer;
        }

        /// <summary>
        /// 增加
        /// </summary>
        /// <param name="answerModel"></param>
        public void AddAnswer(AnswerModel answerModel)
        {
            Answer answer = new Answer();
            ToAnswer(answerModel,answer);
            _answerService.AddEntity(answer);
        }
        /// <summary>
        /// 返回需要编辑的视图
        /// </summary>
        /// <param name="id">传进来的id</param>
        /// <returns></returns>
        public AnswerModel GetEditAnswer(int id)
        {
            AnswerModel answerModel = new AnswerModel();
            return PrepareAnswerModel(answerModel, _answerService.GetEditAnswer(id));
        }

        public void save(FormCollection formCollection)
        {
            Answer answer = new Answer();
            answer.AnswerId = formCollection["AnswerId"];
            answer.AnswerContent = formCollection["AnswerContent"];
            answer.BestNum = Convert.ToInt32(formCollection["BestNum"]);
            _answerService.SaveEdit(answer);
        }
    }
}